<template>
  <div class="bottom-bar">

    <div class="check-content">
      <check-button
          :is-checked="isSelectAll"
          class="check-button"
          @click.native="checkClick"
      />
      <span>全选</span>
    </div>

    <div class="price">
      合计: {{ totalPrice }}
    </div>

    <div class="calculate" @click="calcClick">
      去计算:({{checkedLength}})
    </div>

  </div>
</template>

<script>

import CheckButton from "./CheckButton";
import {mapGetters} from "vuex";

export default {
  name: "CartBottomBar",
  components: {CheckButton},
  computed: {
    ...mapGetters(['cartList']),
    totalPrice() {
      return '￥'+ this.$store.state.cartList.filter(item => {
        return item.checked
      }).reduce((preValue, item) => {
        return  preValue + (item.price * item.count)
      }, 0).toFixed(2)
    },
    checkedLength() {
      return this.$store.state.cartList.filter(item =>  item.checked).length
    },
    isSelectAll() {
      if(this.cartList.length === 0) return false

      //3. find()
      return !(this.cartList.find(item => !item.checked))
      //2. every()
      // return (this.cartList.every(item => item.checked))
      //1.filter  (!item.checked为false的长度)   在取反  不为0就是false
      // return !(this.cartList.filter(item => !item.checked).length)

    }
  },
  methods: {
    checkClick() {
      if(this.isSelectAll) {
        this.cartList.forEach(item => item.checked = false)
      } else {
        this.cartList.forEach(item => item.checked = true)
      }
    },
    calcClick() {
      if(!this.isSelectAll) {
        this.$toast.show('请选择购买的商品', 2000)
      }
    }

  }
}
</script>

<style scoped>

.bottom-bar {
  display: flex;
  align-items: center;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 49px;
  height: 40px;
  background-color: #eee;
}

.check-content {
  display: flex;
  margin-left: 10px;
  width: 100px;
}

.check-button {
  margin-right: 5px;
  width: 20px;
  height: 20px;
}

.price {
  flex: 1;
  margin-left: 20px;
  width: 100px;
}

.calculate {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100px;
  height: 40px;
  color: #fff;
  background-color: red;
}

</style>